*******************************************************************
* This file generates all results based on Bartik trade shocks
* in the main paper and online appendix
*
* Table 4-5, Appendix C
*******************************************************************

cd "${data_dir}"
use hierarchy_IV_data, replace

cd "${results_dir}"

**********************************************
* OLS-FE 
**********************************************

* Main
areg layers logsales i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using Table4.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(logsales) replace



********************
* First Stage
********************
set more off
areg logsales I_WID I_transport_f2 i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 
testparm I_WID I_transport_f2
local Ftest = r(F)
local pval = r(p)

outreg2 using Table4.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append

outreg2 using TableA16.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') replace

cap drop logsaleshat
predict logsaleshat if e(sample) == 1, xbd

areg logsales I_WID I_transport_l2 i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 
testparm I_WID I_transport_l2
local Ftest = r(F)
local pval = r(p)
outreg2 using TableA16.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_l2) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append

areg logsales I_WID I_transport_b2 i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 
testparm I_WID I_transport_b2
local Ftest = r(F)
local pval = r(p)
outreg2 using TableA16.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_b2) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append

areg logsales I_WID I_transport_h2 i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 
testparm I_WID I_transport_h2
local Ftest = r(F)
local pval = r(p)
outreg2 using TableA16.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_h2) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append


* Robustness: First Stage
*****************************************

* Industry-year FE
cap drop industryyear
egen industryyear = group(year sector009) if sample == 1 
quietly tab industryyear, gen(indyeardummy)

areg logsales I_WID I_transport_f2 indyeardummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

cap drop logsaleshat_indyear
predict logsaleshat_indyear if e(sample) == 1, xbd

* industry-trends
forvalues x = 1/9 {
gen trend_sector`x' = (aar - 1998) * (sector009==`x')
}
areg logsales I_WID I_transport_f2 trend_sector1-trend_sector9 ///
i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

cap drop logsaleshat_indtrend
predict logsaleshat_indtrend if e(sample) == 1, xbd

* value added
areg logva I_WID I_transport_f2 i.t sectordummy* if sample == 1, ///
absorb(firmcode) vce(cluster sector027) 

testparm I_WID I_transport_f2
local Ftest = r(F)
local pval = r(p)
outreg2 using TableA22.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append

cap drop logvahat
predict logvahat if e(sample) == 1, xbd


********************
* Main Layers Model
********************

* Panel GMM:
xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) robust ///
artests(3) ivstyle(yeardummy4-yeardummy12)

predict layershat_gmm
predict reslayers_gmm, res
scalar layers_gmm = _b[L.layers]
scalar sales_gmm = _b[logsales]

outreg2 using Table4.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

xtabond2 layers L.layers logsaleshat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat, lag(4 .)) robust ///
artests(3) ivstyle(yeardummy4-yeardummy12)

predict layershat
predict reslayers_iv, res
scalar layers_iv = _b[L.layers]
scalar sales_iv = _b[logsaleshat]

outreg2 using Table4.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


***************************
* Main Inequality Model
***************************

foreach var in wagegap1_topbottom_log log2inequality1 log2inequality2 log2inequality4 log2inequality5 ///
wagegap1_topbottom_res2 res2inequality1_model2 res2inequality2_model2 res2inequality4_model2 res2inequality5_model2 ///
wagegap1_topbottom_res4 res2inequality1_model4 res2inequality2_model4 res2inequality4_model4 res2inequality5_model4 {

areg `var' layershat yeardummy* sectordummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  Table5.tex.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat) adds("Firms", e(df_a)+1) append

scalar `var'_iv = _b[layershat]
}

do "${do_dir}\layerbootstrap_tradeshock.do"

preserve
	local seed = 12345 
	simulate sales_iv=r(sales_iv) layers_iv=r(layers_iv) ///
	log2inequality1_iv=r(log2inequality1_iv) log2inequality2_iv=r(log2inequality2_iv) log2inequality4_iv=r(log2inequality4_iv) log2inequality5_iv=r(log2inequality5_iv) wagegap1_topbottom_log_iv=r(wagegap1_topbottom_log_iv) ///
	res2inequality1_model2_iv=r(res2inequality1_iv) res2inequality2_model2_iv=r(res2inequality2_iv) res2inequality4_model2_iv=r(res2inequality4_iv) res2inequality5_model2_iv=r(res2inequality5_iv) wagegap1_topbottom_res2_iv=r(wagegap1_topbottom_res2_iv) ///
	res2inequality1_model4_iv=r(res2inequality1_iv_v2) res2inequality2_model4_iv=r(res2inequality2_iv_v2) res2inequality4_model4_iv=r(res2inequality4_iv_v2) res2inequality5_model4_iv=r(res2inequality5_iv_v2) wagegap1_topbottom_res4_iv=r(wagegap1_topbottom_res4_iv_v2), ///
	reps(1000) seed(`seed'): layersbootstrap
	
	bstat, stat(sales_iv, layers_iv, ///
	log2inequality1_iv, log2inequality2_iv, log2inequality4_iv, log2inequality5_iv, wagegap1_topbottom_log_iv, ///
	res2inequality1_model2_iv, res2inequality2_model2_iv, res2inequality4_model2_iv, res2inequality5_model2_iv, wagegap1_topbottom_res2_iv, ///
	res2inequality1_model4_iv, res2inequality2_model4_iv, res2inequality4_model4_iv, res2inequality5_model4_iv, wagegap1_topbottom_res4_iv)

	estat bootstrap, all
	outreg2 using Table5_Bootstrap, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) append
restore



foreach var in wagegap1_topbottom_log log2inequality1 log2inequality2 log2inequality4 log2inequality5 ///
wagegap1_topbottom_res2 res2inequality1_model2 res2inequality2_model2 res2inequality4_model2 res2inequality5_model2 ///
wagegap1_topbottom_res4 res2inequality1_model4 res2inequality2_model4 res2inequality4_model4 res2inequality5_model4 {

areg `var' layershat_gmm yeardummy* sectordummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  TableA17.tex.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat_gmm) adds("Firms", e(df_a)+1) append

scalar `var'_gmm = _b[layershat]
}

do "${do_dir}\layerbootstrap_gmm.do"
	
preserve
	local seed = 12345 
	simulate sales_gmm=r(sales_gmm) layers_gmm=r(layers_gmm) ///
	log2inequality1_gmm=r(log2inequality1_gmm) log2inequality2_gmm=r(log2inequality2_gmm) log2inequality4_gmm=r(log2inequality4_gmm) log2inequality5_gmm=r(log2inequality5_gmm) wagegap1_topbottom_log_gmm=r(wagegap1_topbottom_log_gmm) ///
	res2inequality1_model2_gmm=r(res2inequality1_gmm) res2inequality2_model2_gmm=r(res2inequality2_gmm) res2inequality4_model2_gmm=r(res2inequality4_gmm) res2inequality5_model2_gmm=r(res2inequality5_gmm) wagegap1_topbottom_res2_gmm=r(wagegap1_topbottom_res2_gmm) ///
	res2inequality1_model4_gmm=r(res2inequality1_gmm_v2) res2inequality2_model4_gmm=r(res2inequality2_gmm_v2) res2inequality4_model4_gmm=r(res2inequality4_gmm_v2) res2inequality5_model4_gmm=r(res2inequality5_gmm_v2) wagegap1_topbottom_res4_gmm=r(wagegap1_topbottom_res4_gmm_v2), ///
	reps(1000) seed(`seed'): layersbootstrap
	
	bstat, stat(sales_gmm_all, layers_gmm_all, ///
	log2inequality1_gmm, log2inequality2_gmm, log2inequality4_gmm, log2inequality5_gmm, wagegap1_topbottom_log_gmm, ///
	res2inequality1_model2_gmm, res2inequality2_model2_gmm, res2inequality4_model2_gmm, res2inequality5_model2_gmm, wagegap1_topbottom_res2_gmm, ///
	res2inequality1_model4_gmm, res2inequality2_model4_gmm, res2inequality4_model4_gmm, res2inequality5_model4_gmm, wagegap1_topbottom_res4_gmm)

	estat bootstrap, all
	outreg2 using TableA17_Bootstrap, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) append
restore

do "${do_dir}\layerbootstrap_gmmtrade.do"

foreach var in wagegap1_topbottom_log log2inequality1 log2inequality2 log2inequality4 log2inequality5 ///
wagegap1_topbottom_res2 res2inequality1_model2 res2inequality2_model2 res2inequality4_model2 res2inequality5_model2 ///
wagegap1_topbottom_res4 res2inequality1_model4 res2inequality2_model4 res2inequality4_model4 res2inequality5_model4 {

areg `var' layershat_gmm logsaleshat yeardummy* sectordummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  TableA33.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat_gmm logsaleshat) adds("Firms", e(df_a)+1) append

scalar `var'_gmm2 = _b[layershat_gmm]
scalar `var'_gmm2s = _b[logsaleshat]
}

preserve
	local seed = 12345 
	simulate ///
	log2inequality1_gmm2=r(log2inequality1_gmm2) log2inequality2_gmm2=r(log2inequality2_gmm2) log2inequality4_gmm2=r(log2inequality4_gmm2) log2inequality5_gmm2=r(log2inequality5_gmm2) wagegap1_topbottom_log_gmm2=r(wagegap1_topbottom_log_gmm2) ///
	res2inequality1_model2_gmm2=r(res2inequality1_gmm2) res2inequality2_model2_gmm2=r(res2inequality2_gmm2) res2inequality4_model2_gmm2=r(res2inequality4_gmm2) res2inequality5_model2_gmm2=r(res2inequality5_gmm2) wagegap1_topbottom_res2_gmm2=r(wagegap1_topbottom_res2_gmm2) ///
	res2inequality1_model4_gmm2=r(res2inequality1_gmm2_v2) res2inequality2_model4_gmm2=r(res2inequality2_gmm2_v2) res2inequality4_model4_gmm2=r(res2inequality4_gmm2_v2) res2inequality5_model4_gmm2=r(res2inequality5_gmm2_v2) wagegap1_topbottom_res4_gmm2=r(wagegap1_topbottom_res4_gmm2_v2) ///
	log2inequality1_gmm2s=r(log2inequality1_gmm2s) log2inequality2_gmm2s=r(log2inequality2_gmm2s) log2inequality4_gmm2s=r(log2inequality4_gmm2s) log2inequality5_gmm2s=r(log2inequality5_gmm2s) wagegap1_topbottom_log_gmm2s=r(wagegap1_topbottom_log_gmm2s) ///
	res2inequality1_model2_gmm2s=r(res2inequality1_gmm2s) res2inequality2_model2_gmm2s=r(res2inequality2_gmm2s) res2inequality4_model2_gmm2s=r(res2inequality4_gmm2s) res2inequality5_model2_gmm2s=r(res2inequality5_gmm2s) wagegap1_topbottom_res2_gmm2s=r(wagegap1_topbottom_res2_gmm2s) ///
	res2inequality1_model4_gmm2s=r(res2inequality1_gmm2s_v2) res2inequality2_model4_gmm2s=r(res2inequality2_gmm2s_v2) res2inequality4_model4_gmm2s=r(res2inequality4_gmm2s_v2) res2inequality5_model4_gmm2s=r(res2inequality5_gmm2s_v2) wagegap1_topbottom_res4_gmm2s=r(wagegap1_topbottom_res4_gmm2s_v2), ///
	reps(1000) seed(`seed'): layersbootstrap
	
	bstat, stat( ///
	log2inequality1_gmm2, log2inequality2_gmm2, log2inequality4_gmm2, log2inequality5_gmm2, wagegap1_topbottom_log_gmm2, ///
	res2inequality1_model2_gmm2, res2inequality2_model2_gmm2, res2inequality4_model2_gmm2, res2inequality5_model2_gmm2, wagegap1_topbottom_res2_gmm2, ///
	res2inequality1_model4_gmm2, res2inequality2_model4_gmm2, res2inequality4_model4_gmm2, res2inequality5_model4_gmm2, wagegap1_topbottom_res4_gmm2, ///
	log2inequality1_gmm2s, log2inequality2_gmm2s, log2inequality4_gmm2s, log2inequality5_gmm2s, wagegap1_topbottom_log_gmm2s, ///
	res2inequality1_model2_gmm2s, res2inequality2_model2_gmm2s, res2inequality4_model2_gmm2s, res2inequality5_model2_gmm2s, wagegap1_topbottom_res2_gmm2s, ///
	res2inequality1_model4_gmm2s, res2inequality2_model4_gmm2s, res2inequality4_model4_gmm2s, res2inequality5_model4_gmm2s, wagegap1_topbottom_res4_gmm2s)

	estat bootstrap, all
	outreg2 using TableA33_Bootstrap, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) append
restore

**********************************************
* Layers Model: Residual Analysis
**********************************************
* Note: xtabond reports residuals including firm FE, so I run another
* firm FE regression to extract the true residual:
local var reslayers_iv 
areg `var' if sample == 1, absorb(firmcode)
cap drop reslayers2
predict reslayers2, res

preserve
keep if sample == 1

sort logsales
cap drop bin
gen bin = ceil(200*_n/_N)
cap drop meanres*
bys bin: egen meanres = mean(reslayers2)
bys bin: egen meansales = mean(logsales)

twoway (lpoly reslayers2 logsales if sample == 1 & bin > 1 & bin <200, degree(0)) ///
(scatter meanres meansales if sample == 1 & bin > 1 & bin <200), legend(off) ///
ytitle("Residual Layers") xtitle(Log Sales) ///
title("Residual Analysis: Layers") ///
graphregion(color(white)) yscale(range(-0.075(0.025)0.075)) ///
ylabel(-0.05(0.05)0.05)

graph export FigureA4_sales.pdf, replace

restore

preserve
sum empl2 if sample==1, det
gen logempl2 = log(empl2)
keep if sample == 1 & empl2 >= 5

sort logempl2
cap drop bin
gen bin = ceil(100*_n/_N)
cap drop meanres*
bys bin: egen meanres = mean(reslayers2)
bys bin: egen meansales = mean(logempl2)

twoway (lpoly reslayers2 logempl2 if sample == 1 & bin > 1 & bin <100, degree(0)) ///
(scatter meanres meansales if sample == 1 & bin > 1 & bin <100), legend(off) ///
ytitle("Residual Layers") xtitle(Log Employment) ///
title("Residual Analysis: Layers") ///
graphregion(color(white)) yscale(range(-0.075(0.025)0.075)) ///
ylabel(-0.05(0.05)0.05)

graph export FigureA4_fteempl.pdf, replace

restore


**********************************************
* Robustness on GMM model choices
**********************************************

* set maximum number of lags:

* 1) use only one lag:
xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 3)) ///
gmmstyle(logsales, lag(5 5)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA29.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) replace

* 2) use two lags:
xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 4)) ///
gmmstyle(logsales, lag(5 6)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA29.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


* 3) use three lags:
xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 5)) ///
gmmstyle(logsales, lag(5 7)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA29.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

* 2) use four lags:
xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 6)) ///
gmmstyle(logsales, lag(5 8)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA29.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

* Original:
xtabond2 layers L.layers logsales sectordummy1-sectordummy8 ///
yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA29.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


* trade shock iv:

* 1) use only one lag:
xtabond2 layers L.layers logsaleshat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 3)) ///
gmmstyle(logsaleshat, lag(4 4)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA30.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) replace

* 2) use two lags:
xtabond2 layers L.layers logsaleshat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 4)) ///
gmmstyle(logsaleshat, lag(4 5)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA30.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


* 3) use three lags:
xtabond2 layers L.layers logsaleshat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 5)) ///
gmmstyle(logsaleshat, lag(4 6)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA30.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

* 2) use four lags:
xtabond2 layers L.layers logsaleshat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 6)) ///
gmmstyle(logsaleshat, lag(4 7)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA30.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

* Original:
xtabond2 layers L.layers logsaleshat sectordummy1-sectordummy8 ///
yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat, lag(4 .)) ///
ivstyle(yeardummy4-yeardummy12) robust artests(3)

outreg2 using TableA30.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


**************************************************
* Robustness: Value Added
**************************************************

* Value added
areg layers logva i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using TableA22.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(logva) append


xtabond2 layers L.layers logvahat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logvahat, lag(4 .)) robust ///
artests(3) ivstyle(yeardummy4-yeardummy12)

gen vasample = e(sample)==1

xtabond2 layers L.layers logva ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if vasample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logva, lag(4 .)) robust ///
artests(3) ivstyle(yeardummy4-yeardummy12)

outreg2 using TableA22.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logva) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


xtabond2 layers L.layers logvahat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logvahat, lag(4 .)) robust ///
artests(3) ivstyle(yeardummy4-yeardummy12)

outreg2 using TableA22.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logvahat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

predict layershat_va
scalar layers_ivva = _b[L.layers]
scalar sales_ivva = _b[logvahat]

* Inequality:
foreach var in wagegap1_topbottom_log log2inequality1 log2inequality2 log2inequality4 log2inequality5 ///
wagegap1_topbottom_res2 res2inequality1_model2 res2inequality2_model2 res2inequality4_model2 res2inequality5_model2 ///
wagegap1_topbottom_res4 res2inequality1_model4 res2inequality2_model4 res2inequality4_model4 res2inequality5_model4 {

areg `var' layershat_va yeardummy* sectordummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  TableA23.tex.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat_va) adds("Firms", e(df_a)+1) append

scalar `var'_ivva = _b[layershat_va]
}

do "${do_dir}\layerbootstrap_tradeshockva.do"

preserve
	local seed = 12345 
	simulate sales_ivva=r(sales_ivva) layers_ivva=r(layers_ivva) ///
	log2inequality1_ivva=r(log2inequality1_ivva) log2inequality2_ivva=r(log2inequality2_ivva) log2inequality4_ivva=r(log2inequality4_ivva) log2inequality5_ivva=r(log2inequality5_ivva) wagegap1_topbottom_log_ivva=r(wagegap1_topbottom_log_ivva) ///
	res2inequality1_model2_ivva=r(res2inequality1_ivva) res2inequality2_model2_ivva=r(res2inequality2_ivva) res2inequality4_model2_ivva=r(res2inequality4_ivva) res2inequality5_model2_ivva=r(res2inequality5_ivva) wagegap1_topbottom_res2_ivva=r(wagegap1_topbottom_res2_ivva) ///
	res2inequality1_model4_ivva=r(res2inequality1_ivva_v2) res2inequality2_model4_ivva=r(res2inequality2_ivva_v2) res2inequality4_model4_ivva=r(res2inequality4_ivva_v2) res2inequality5_model4_ivva=r(res2inequality5_ivva_v2) wagegap1_topbottom_res4_ivva=r(wagegap1_topbottom_res4_ivva_v2), ///
	reps(1000) seed(`seed'): layersbootstrap
	
	bstat, stat(sales_ivva, layers_ivva, ///
	log2inequality1_ivva, log2inequality2_ivva, log2inequality4_ivva, log2inequality5_ivva, wagegap1_topbottom_log_ivva, ///
	res2inequality1_model2_ivva, res2inequality2_model2_ivva, res2inequality4_model2_ivva, res2inequality5_model2_ivva, wagegap1_topbottom_res2_ivva, ///
	res2inequality1_model4_ivva, res2inequality2_model4_ivva, res2inequality4_model4_ivva, res2inequality5_model4_ivva, wagegap1_topbottom_res4_ivva)

	estat bootstrap, all
	outreg2 using TableA23_Bootstrap, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) append
restore


**********************************************
* Robustness: Lagged effects on layers
**********************************************

 * MAIN First Stage (Benchmark)
areg logsales I_WID I_transport_f2 i.t sectordummy* ///
if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using TableA25.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1) replace

areg logsales I_WID L.I_WID I_transport_f2 i.t sectordummy* ///
if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using TableA25.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID L.I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1) append

areg logsales I_WID L.I_WID I_transport_f2 L.I_transport_f2 i.t sectordummy* ///
if sample == 1, absorb(firmcode) vce(cluster sector027) 

cap drop logsaleshatlag
predict logsaleshatlag if e(sample) == 1, xbd

outreg2 using TableA25.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID L.I_WID I_transport_f2 L.I_transport_f2) ///
adds("Firms", e(df_a)+1) append

* Include lagged sales into the model
xtabond2 layers L.layers logsales L.logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) ///
gmmstyle(L.logsales, lag(4 .)) robust ///
noconstant artests(3) ivstyle(yeardummy4-yeardummy12)

test logsales + L.logsales = 0

outreg2 using TableA26.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales L.logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) replace


xtabond2 layers L.layers logsaleshat L.logsaleshat ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat, lag(4 .)) ///
gmmstyle(L.logsaleshat, lag(4 .)) robust ///
noconstant artests(3) ivstyle(yeardummy4-yeardummy12)

test logsaleshat + L.logsaleshat = 0

outreg2 using TableA26.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat L.logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

* Use lagged trade shock as an instrument:
xtabond2 layers L.layers logsaleshatlag ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshatlag, lag(4 .)) robust ///
artests(3) ivstyle(yeardummy4-yeardummy12)

outreg2 using TableA26.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshatlag) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append



**************************************************
* Role of skill composition:
**************************************************
set more off
xtset firmcode aar
foreach var in collegeshare avschool avexperience sdschool sdexperience { 
areg `var' I_WID I_transport_f2 i.t sectordummy* if sample == 1, ///
absorb(firmcode) vce(cluster sector027)

* Clearly weak IV
testparm I_WID I_transport_f2
local Fval = r(F)
local pval = r(p)

outreg2 using TableA34.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1,"F-Test", `Fval', "p-value of F-Test", `pval') append

}

**************************************************
* Role of investment and innovation:
**************************************************
gen logk = log(fixedassets)

gen loginv_p1 = log(dtam + taad + 1)
gen loginv2_p1 = log(dtam + 1)
gen loginv3_p1 = log(taad + 1)

gen logcap  = log(maat)
gen logcap_intang  = log(iaat)
gen logcap_tang  = log(atam + aadi)
xtset firmcode aar

set more off
foreach var in logcap logcap_intang logcap_tang loginv_p1 loginv2_p1 loginv3_p1{
areg `var' I_WID I_transport_f2 ///
i.t sectordummy* if sample == 1, ///
absorb(firmcode) vce(cluster sector027)

testparm I_WID I_transport_f2
local Fval = r(F)
local pval = r(p)

outreg2 using TableA35.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1,"F-Test", `Fval', "p-value of F-Test", `pval') append
}

set more off
foreach var in first_hs6 first_hs2 {
areg `var' I_WID I_transport_f2 ///
i.t sectordummy* if sample == 1 & first_hs6 != ., ///
absorb(firmcode) vce(cluster sector027)

* Check weak IV
testparm I_WID I_transport_f2
local Fval = r(F)
local pval = r(p)

outreg2 using TableA35.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID I_transport_f2) ///
adds("Firms", e(df_a)+1,"F-Test", `Fval', "p-value of F-Test", `pval') append
}

**********************************************
* Industry Trends
**********************************************

* Industry-specific trends
areg layers logsales i.t sectordummy* trend_sector1-trend_sector9 if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using TableA19.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(logsales) replace

xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
trend_sector1-trend_sector9 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) ///
ivstyle(yeardummy4-yeardummy12) ivstyle(trend_sector1-trend_sector9) robust artests(3)

outreg2 using TableA19.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

xtabond2 layers L.layers logsaleshat_indtrend ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
trend_sector1-trend_sector9 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat_indtrend, lag(4 .)) ///
ivstyle(yeardummy4-yeardummy12) ivstyle(trend_sector1-trend_sector9) robust artests(3)

outreg2 using TableA19.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat_indtrend) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append



* Industry-Year FE:
* define indicators for Arellano Bond
cap drop industryyear
egen industryyear = group(year sector009) if sample == 1 ///
& L.layers != . & year != 2000 & sector009 != 2
tab industryyear
cap drop indyeardummy*
quietly tab industryyear, gen(indyeardummy)

areg layers logsales indyeardummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using TableA19.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(logsales) append

xtabond2 layers L.layers logsales ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
indyeardummy1-indyeardummy51 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) ///
ivstyle(yeardummy4-yeardummy12) ///
ivstyle(indyeardummy1-indyeardummy51) robust artests(3)

outreg2 using TableA19.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

xtabond2 layers L.layers logsaleshat_indyear ///
sectordummy1-sectordummy8 yeardummy4-yeardummy12 ///
indyeardummy1-indyeardummy51 if sample == 1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat_indyear, lag(4 .)) ///
ivstyle(yeardummy4-yeardummy12) ///
ivstyle(indyeardummy1-indyeardummy51) robust artests(3)

outreg2 using TableA19.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat_indyear) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

cap drop layershat_indyear
predict layershat_indyear

* Inequality:
areg wagegap1_topbottom_log layershat_indyear indyeardummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  TableA21.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat_indyear) adds("Firms", e(df_a)+1) replace

foreach var in log2inequality1 log2inequality2 log2inequality4 log2inequality5 ///
wagegap1_topbottom_res2 res2inequality1_model2 res2inequality2_model2 res2inequality4_model2 res2inequality5_model2 ///
wagegap1_topbottom_res4 res2inequality1_model4 res2inequality2_model4 res2inequality4_model4 res2inequality5_model4 {

areg `var' layershat_indyear indyeardummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  TableA21.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat_indyear) adds("Firms", e(df_a)+1) append
}


* Results By Sector:
set more off
foreach x in 2 4 5 6 7 {
if `x' != 5 {
xtabond2 layers L.layers logsales yeardummy* if sample == 1 & sectordummy`x'==1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) ivstyle(yeardummy4-yeardummy12) ///
robust artests(3)

outreg2 using TableA20.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append
}
if `x' == 5 {
xtabond2 layers L.layers logsales yeardummy* if sample == 1 & sectordummy`x'==1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(3 .)) ivstyle(yeardummy4-yeardummy12) ///
robust artests(3)

outreg2 using TableA20.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append
}

}

* Results By Manufacturing Subsector:
foreach x in 15 17 20 23 27 36{
xtabond2 layers L.layers logsales yeardummy* if sample == 1 & sector027==`x', ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsales, lag(5 .)) ivstyle(yeardummy4-yeardummy12) ///
robust artests(3)

outreg2 using TableA20.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

}


foreach x in 2 4 5 6 7 {
xtabond2 layers L.layers logsaleshat yeardummy* if sample == 1 & sectordummy`x'==1, ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat, lag(4 .)) ivstyle(yeardummy4-yeardummy12) ///
robust artests(3)

outreg2 using TableA20_PanelB.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

}

tab sector027 if sample == 1 & sector009==2

set more off
foreach x in 15 17 20 23 27 36{
xtabond2 layers L.layers logsaleshat yeardummy* if sample == 1 & sector027==`x', ///
gmmstyle(L.layers, lag(3 .)) gmmstyle(logsaleshat, lag(4 .)) ivstyle(yeardummy4-yeardummy12) ///
robust artests(3)

outreg2 using TableA20_PanelB.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers logsaleshat) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append

}


**********************************************
* Alternative Layers Measures
**********************************************

set more off
foreach var in 3max alt{

areg layers_`var' logsales i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

outreg2 using TableA27.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(logsales) ///
adds("Firms", e(df_a)+1) append


xtabond2 layers_`var' L.layers_`var' logsales sectordummy1-sectordummy8 ///
yeardummy4-yeardummy12 if sample == 1, ///
gmmstyle(L.layers_`var', lag(7 .)) gmmstyle(logsales, lag(5 .)) ///
robust artests(3) ivstyle(yeardummy4-yeardummy12)

cap drop layershat_`var'
predict layershat_`var'

outreg2 using TableA27.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(L.layers_`var' logsales) ///
adds("Firms", e(N_g), "AB test", e(ar3p), "J test", e(hansenp)) append


foreach var2 in wagegap1_topbottom_log log2inequality1 log2inequality2 log2inequality4 log2inequality5 {
areg `var2' layershat_`var' yeardummy* sectordummy* if sample0 == 1, absorb(firmcode) vce(cluster sector027)
outreg2 using  TableA28_`var'.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) ///
keep(layershat_`var') adds("Firms", e(df_a)+1) append
}

}

**********************************************
* Count Models
**********************************************

areg logsales I_WID I_transport_f2 i.t if sample == 1, absorb(firmcode) vce(cluster sector027) 
cap drop logsaleshat_simple
predict logsaleshat_simple if e(sample) == 1, xbd

set more off
cap drop layers0
gen layers0 = layers - 1

xtset firmcode aar
forvalues x = 1/10 {
cap drop logsales`x'
gen logsales`x' = L`x'.logsales
}
forvalues x = 1/10 {
cap drop layers`x'
gen layers`x' = L`x'.layers0
}

forvalues x = 1999/2008 {
cap drop dyear`x'
gen dyear`x' = (aar == `x')
}

* Static Poisson
xtpoisson layers0 logsales yeardummy* if sample == 1, fe vce(robust)

outreg2 using TableA24.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(layers logsales) adds("Firms", e(N_g)) replace

* Dynamic Poisson with fixed effects: exponential feedback (Chamberlain 92, Wooldridge 97)
cap program drop gmm_poipre2
program gmm_poipre2
version 15
syntax varlist if, at(name) myrhs(varlist) mylhs(varlist) myidvar(varlist)

quietly{
tempvar mu mubar ybar
gen double `mu' = 0 `if'
local j = 1
foreach var of varlist `myrhs' {
replace `mu' = `mu' + `var'*`at'[1,`j'] `if'
local j = `j' + 1
}

replace `mu' = exp(`mu')
replace `varlist' = (`mylhs' * (L.`mu' /`mu')) - L.`mylhs' `if'
}
end

global XLIST layers1 logsales dyear2001 dyear2002 dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008
* just-identified
global IVLIST layers1 logsales dyear2001 dyear2002 dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008

gmm gmm_poipre2 if sample == 1, mylhs(layers0) myrhs($XLIST) myidvar(firmcode) nequations(1) ///
	parameters($XLIST) instruments($IVLIST, noconstant) onestep ///
	vce(cluster firmcode)
outreg2 using TableA24_model1.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) adds("Firms", e(N_clust)) append

* trade IV for sales, long lagged layers IV for L.layers

global XLIST2 layers1 logsales dyear2001 dyear2002 dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008
* just-identified
global IVLIST2 layers2 logsaleshat_simple dyear2001 dyear2002 dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008

gmm gmm_poipre2 if sample == 1, mylhs(layers0) myrhs($XLIST2) myidvar(firmcode) nequations(1) ///
	parameters($XLIST2) instruments($IVLIST2, noconstant) onestep ///
	vce(cluster firmcode)
outreg2 using TableA24_model2.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) adds("Firms", e(N_clust)) append

global XLIST3 layers1 logsales dyear2002 dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008
global IVLIST3 layers3 logsaleshat_simple dyear2002 dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008

gmm gmm_poipre2 if sample == 1, mylhs(layers0) myrhs($XLIST3) myidvar(firmcode) nequations(1) ///
	parameters($XLIST3) instruments($IVLIST3, noconstant) onestep ///
	vce(cluster firmcode)
outreg2 using TableA24_model3.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) adds("Firms", e(N_clust)) append	

global XLIST4 layers1 logsales dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008
global IVLIST4 layers4 logsaleshat_simple dyear2003 dyear2004 dyear2005 dyear2006 dyear2007 dyear2008

gmm gmm_poipre2 if sample == 1, mylhs(layers0) myrhs($XLIST4) myidvar(firmcode) nequations(1) ///
	parameters($XLIST4) instruments($IVLIST4, noconstant) onestep ///
	vce(cluster firmcode)
outreg2 using TableA24_model4.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) adds("Firms", e(N_clust)) append




*******************************
* Heterogeneity by Firm Types
*******************************
cd "${data_dir}"
use hierarchy_IV_data, replace

cd "${results_dir}"

* Layers and number of plants
gen plantcode = plants
replace plantcode = 4 if plants >= 4

* Layers and number of products
cap drop productcode
gen productcode = NHS62
replace productcode = 5 if NHS62 > 4

gen multiplant = plants > 1 & plants != .
gen multiproduct = NHS62 > 1 & NHS62 != .

* Not offshoring:
gen notoffshoring_hs4 = (offshoring_hs4 == 0)
gen notoffshoring_hs6 = (offshoring_hs6 == 0)

bys firmcode: egen keeper = max(sample)
keep if keeper == 1
keep if aar > 1998 & aar < 2009
xtset firmcode t

gen sample1 = sample==1 & empl1>4
cap drop sample3
gen sample3 = sample==1 & empl1>19

gen sampleyoung = sample==1 & firmage <= 10
gen sampleold = sample==1 & firmage >= 25

rename I_transport_f2 I_transport2

xtreg logsales I_WID I_transport2 i.t sectordummy* if sample == 1, fe vce(cluster firmcode) 
cap drop logsaleshat
predict logsaleshat if e(sample) == 1, xbu

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & sampleyoung == 1, ///
gmmstyle(L.layers, lag(3 .) ) ///
gmmstyle(logsales, lag(5 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_sampleyoung = _b[L.layers]
scalar sales_sampleyoung = _b[logsales]
predict layershat_sampleyoung
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) replace

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & sampleold == 1, ///
gmmstyle(L.layers, lag(3 .) ) ///
gmmstyle(logsales, lag(5 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_sampleold = _b[L.layers]
scalar sales_sampleold = _b[logsales]
predict layershat_sampleold
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) append

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & sample3 == 1, ///
gmmstyle(L.layers, lag(3 .) ) ///
gmmstyle(logsales, lag(5 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_sample3 = _b[L.layers]
scalar sales_sample3 = _b[logsales]
predict layershat_sample3
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) append

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & sample1 == 1, ///
gmmstyle(L.layers, lag(3 .) ) ///
gmmstyle(logsales, lag(5 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_sample1 = _b[L.layers]
scalar sales_sample1 = _b[logsales]
predict layershat_sample1
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) append

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & notoffshoring_hs6 == 0, ///
gmmstyle(L.layers, lag(3 .) ) ///
gmmstyle(logsales, lag(5 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_offshoring_hs6 = _b[L.layers]
scalar sales_offshoring_hs6 = _b[logsales]
predict layershat_offshoring_hs6
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) append

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & multiplant == 1, ///
gmmstyle(L.layers, lag(2 .) ) ///
gmmstyle(logsales, lag(4 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_multiplant = _b[L.layers]
scalar sales_multiplant = _b[logsales]
predict layershat_multiplant
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) append

xtabond2 layers L.layers logsales yeardummy* sectordummy* if sample == 1 & multiproduct == 1, ///
gmmstyle(L.layers, lag(3 .) ) ///
gmmstyle(logsales, lag(4 .) ) ///
robust noconstant artests(3) ivstyle(yeardummy4-yeardummy12)
scalar layers_multiproduct = _b[L.layers]
scalar sales_multiproduct = _b[logsales]
predict layershat_multiproduct
outreg2 using TableA31.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) ///
bdec(4) keep(L.layers logsales) adds(AB test, e(ar3p), Hansen J test, e(hansenp)) append

* Young firms
*************************************
	areg log2inequality1 layershat_sampleyoung yeardummy* sectordummy* if sample0==1 & sampleyoung == 1, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sampleyoung) adds("Firms", e(df_a)+1) replace
	scalar log2inequality1l_sampleyoung = _b[layershat_sampleyoung]
	areg log2inequality4 layershat_sampleyoung yeardummy* sectordummy* if sample0==1 & sampleyoung == 1, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sampleyoung) adds("Firms", e(df_a)+1) replace	
	scalar log2inequality4l_sampleyoung = _b[layershat_sampleyoung]
	areg wagegap1_topbottom_log layershat_sampleyoung yeardummy* sectordummy* if sample0==1 & sampleyoung == 1, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sampleyoung) adds("Firms", e(df_a)+1) replace	
	scalar log2inequality5l_sampleyoung = _b[layershat_sampleyoung]

	* Old firms
*************************************
	areg log2inequality1 layershat_sampleold logsaleshat yeardummy* sectordummy* if sample0==1 & sampleold == 1, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sampleold) adds("Firms", e(df_a)+1) append
	scalar log2inequality1l_sampleold = _b[layershat_sampleold]
	areg log2inequality4 layershat_sampleold logsaleshat yeardummy* sectordummy* if sample0==1 & sampleold == 1, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sampleold) adds("Firms", e(df_a)+1) append	
	scalar log2inequality4l_sampleold = _b[layershat_sampleold]
	areg wagegap1_topbottom_log layershat_sampleold logsaleshat yeardummy* sectordummy* if sample0==1 & sampleold == 1, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sampleold) adds("Firms", e(df_a)+1) append	
	scalar log2inequality5l_sampleold = _b[layershat_sampleold]
	
* Firms with at least 20 workers
*************************************
	areg log2inequality1 layershat_sample3 yeardummy* sectordummy* if sample0==1 & sample3 == 1, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sample3) adds("Firms", e(df_a)+1) append
	scalar log2inequality1l_sample3 = _b[layershat_sample3]
	areg log2inequality4 layershat_sample3 yeardummy* sectordummy* if sample0==1 & sample3 == 1, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sample3) adds("Firms", e(df_a)+1) append	
	scalar log2inequality4l_sample3 = _b[layershat_sample3]
	areg wagegap1_topbottom_log layershat_sample3 yeardummy* sectordummy* if sample0==1 & sample3 == 1, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sample3) adds("Firms", e(df_a)+1) append	
	scalar log2inequality5l_sample3 = _b[layershat_sample3]
	
	* Firms with at least 5 workers
	*************************************
	areg log2inequality1 layershat_sample1 yeardummy* sectordummy* if sample0==1 & sample1 == 1, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sample1) adds("Firms", e(df_a)+1) append	
	scalar log2inequality1l_sample1 = _b[layershat_sample1]
	areg log2inequality4 layershat_sample1 yeardummy* sectordummy* if sample0==1 & sample1 == 1, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sample1) adds("Firms", e(df_a)+1) append	
	scalar log2inequality4l_sample1 = _b[layershat_sample1]
	areg wagegap1_topbottom_log layershat_sample1 yeardummy* sectordummy* if sample0==1 & sample1 == 1, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_sample1) adds("Firms", e(df_a)+1) append	
	scalar log2inequality5l_sample1 = _b[layershat_sample1]

	* Offshoring (HS6) firms
	*************************************
	areg log2inequality1 layershat_offshoring_hs6 yeardummy* sectordummy* if sample0==1 & notoffshoring_hs6 == 0, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_offshoring_hs6) adds("Firms", e(df_a)+1) append	
	scalar log2inequality1l_offshoring_hs6 = _b[layershat_offshoring_hs6]
	areg log2inequality4 layershat_offshoring_hs6 yeardummy* sectordummy* if sample0==1 & notoffshoring_hs6 == 0, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_offshoring_hs6) adds("Firms", e(df_a)+1) append	
	scalar log2inequality4l_offshoring_hs6 = _b[layershat_offshoring_hs6]
	areg wagegap1_topbottom_log layershat_offshoring_hs6 yeardummy* sectordummy* if sample0==1 & notoffshoring_hs6 == 0, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_offshoring_hs6) adds("Firms", e(df_a)+1) append	
	scalar log2inequality5l_offshoring_hs6 = _b[layershat_offshoring_hs6]
	
	* multiplant firms
	*************************************
	areg log2inequality1 layershat_multiplant yeardummy* sectordummy* if sample0==1 & multiplant == 1, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_multiplant) adds("Firms", e(df_a)+1) append	
	scalar log2inequality1l_multiplant = _b[layershat_multiplant]
	areg log2inequality4 layershat_multiplant yeardummy* sectordummy* if sample0==1 & multiplant == 1, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_multiplant) adds("Firms", e(df_a)+1) append	
	scalar log2inequality4l_multiplant = _b[layershat_multiplant]
	areg wagegap1_topbottom_log layershat_multiplant yeardummy* sectordummy* if sample0==1 & multiplant == 1, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_multiplant) adds("Firms", e(df_a)+1) append	
	scalar log2inequality5l_multiplant = _b[layershat_multiplant]
	
	* multiproduct firms
	*************************************
	areg log2inequality1 layershat_multiproduct yeardummy* sectordummy* if sample0==1 & multiproduct == 1, absorb(firmcode)
	outreg2 using TableA32_PanelB.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_multiproduct) adds("Firms", e(df_a)+1) append	
	scalar log2inequality1l_multiproduct = _b[layershat_multiproduct]
	areg log2inequality4 layershat_multiproduct yeardummy* sectordummy* if sample0==1 & multiproduct == 1, absorb(firmcode)
	outreg2 using TableA32_PanelC.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_multiproduct) adds("Firms", e(df_a)+1) append	
	scalar log2inequality4l_multiproduct = _b[layershat_multiproduct]
	areg wagegap1_topbottom_log layershat_multiproduct yeardummy* sectordummy* if sample0==1 & multiproduct == 1, absorb(firmcode)
	outreg2 using TableA32_PanelA.tex, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) keep(layershat_multiproduct) adds("Firms", e(df_a)+1) append	
	scalar log2inequality5l_multiproduct = _b[layershat_multiproduct]
	
	
	set more off 
	do "${do_dir}\layerbootstrap_tradeshock_heterogeneous.do"
	preserve
	local seed = 12345
	simulate sales_multiplant=r(sales_multiplant) layers_multiplant=r(layers_multiplant) ///
	log2inequality1l_multiplant=r(log2inequality1l_multiplant) log2inequality4l_multiplant=r(log2inequality4l_multiplant) log2inequality5l_multiplant=r(log2inequality5l_multiplant) ///
	sales_multiproduct=r(sales_multiproduct) layers_multiproduct=r(layers_multiproduct) ///
	log2inequality1l_multiproduct=r(log2inequality1l_multiproduct) log2inequality4l_multiproduct=r(log2inequality4l_multiproduct) log2inequality5l_multiproduct=r(log2inequality5l_multiproduct) ///
	sales_offshoring_hs6=r(sales_offshoring_hs6) layers_offshoring_hs6=r(layers_offshoring_hs6) ///
	log2inequality1l_offshoring_hs6=r(log2inequality1l_offshoring_hs6) log2inequality4l_offshoring_hs6=r(log2inequality4l_offshoring_hs6) log2inequality5l_offshoring_hs6=r(log2inequality5l_offshoring_hs6) ///
	sales_sampleyoung=r(sales_sampleyoung) layers_sampleyoung=r(layers_sampleyoung) ///
	log2inequality1l_sampleyoung=r(log2inequality1l_sampleyoung) log2inequality4l_sampleyoung=r(log2inequality4l_sampleyoung) log2inequality5l_sampleyoung=r(log2inequality5l_sampleyoung) ///
	sales_sampleold=r(sales_sampleold) layers_sampleold=r(layers_sampleold) ///
	log2inequality1l_sampleold=r(log2inequality1l_sampleold) log2inequality4l_sampleold=r(log2inequality4l_sampleold) log2inequality5l_sampleold=r(log2inequality5l_sampleold) ///
	sales_sample1=r(sales_sample1) layers_sample1=r(layers_sample1) ///
	log2inequality1l_sample1=r(log2inequality1l_sample1) log2inequality4l_sample1=r(log2inequality4l_sample1) log2inequality5l_sample1=r(log2inequality5l_sample1) ///
	sales_sample3=r(sales_sample3) layers_sample3=r(layers_sample3) ///
	log2inequality1l_sample3=r(log2inequality1l_sample3) log2inequality4l_sample3=r(log2inequality4l_sample3) log2inequality5l_sample3=r(log2inequality5l_sample3), ///
	reps(1000) seed(`seed'): layersbootstrap
	
	bstat, stat(sales_multiplant, layers_multiplant, ///
	log2inequality1l_multiplant, log2inequality4l_multiplant, log2inequality5l_multiplant, ///
	sales_multiproduct, layers_multiproduct, ///
	log2inequality1l_multiproduct, log2inequality4l_multiproduct, log2inequality5l_multiproduct, ///
	sales_offshoring_hs6, layers_offshoring_hs6, ///
	log2inequality1l_offshoring_hs6, log2inequality4l_offshoring_hs6, log2inequality5l_offshoring_hs6, ///
	sales_sampleyoung, layers_sampleyoung, ///
	log2inequality1l_sampleyoung, log2inequality4l_sampleyoung, log2inequality5l_sampleyoung, ///
	sales_sampleold, layers_sampleold, ///
	log2inequality1l_sampleold, log2inequality4l_sampleold, log2inequality5l_sampleold, ///
	sales_sample1, layers_sample1, ///
	log2inequality1l_sample1, log2inequality4l_sample1, log2inequality5l_sample1, ///
	sales_sample3, layers_sample3, ///
	log2inequality1l_sample3, log2inequality4l_sample3, log2inequality5l_sample3)

	estat bootstrap, all
	outreg2 using TableA32_bootstrap, tex alpha(0.01, 0.05, 0.1) symbol(***,**,*) bdec(4) append
	restore
	
	
**********************************************
*** First stage for Bartik diagnostics
**********************************************
cd "${data_dir}"
use hierarchy_IV_data.dta, clear

tostring aar, replace
merge 1:1 cvrnr aar using firmsample_prio1000_Bartik_diagnostics, keepusing(shock)
drop if _merge == 2
drop _merge
destring aar, replace
xtset firmcode aar
rename shock I_WID_new

cd "${results_dir}"

set more off
areg logsales I_WID_new i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

testparm I_WID_new
local Ftest = r(F)
local pval = r(p)
outreg2 using Table_Bartik.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID_new) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') replace


areg logsales I_WID i.t sectordummy* if e(sample) == 1, absorb(firmcode) vce(cluster sector027) 

testparm I_WID
local Ftest = r(F)
local pval = r(p)
outreg2 using Table_Bartik.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append


areg logsales I_WID i.t sectordummy* if sample == 1, absorb(firmcode) vce(cluster sector027) 

testparm I_WID
local Ftest = r(F)
local pval = r(p)
outreg2 using Table_Bartik.tex, tex alpha(0.01, 0.05, 0.1) ///
symbol(***,**,*) bdec(4) keep(I_WID) ///
adds("Firms", e(df_a)+1,"F-Test",`Ftest',"p-value",`pval') append


********************************
* Bartik Diagnosis Tests
*
* Nov 26 2019
********************************
clear all

set maxvar 32767
cd "${data_dir}"

/* Load main hierarchy data at the firm-year level */
use firmsample_prio1000_Bartik_diagnostics, clear

cap drop t
keep if sample == 1
destring aar, replace
tab aar

/*
Load data on product-destination shares by firm-year (incl zeros) 

Note: productdest_shares is a fully expanded set of firm-product-dests
		for all pairs that at least one firm sells to in the initial period.
		This means I need to keep all "using" data
		
Note 2: Since the shares are fixed initially, the merge is only by cvrnr.
		To make it clean, I merge by year.
 */
forvalues x = 1999/2008 {
preserve
keep if aar == `x'
merge 1:m cvrnr using firmsample_prio1000_productdest_shares, gen(merge_shares)

* drop those firms without exports to the top 1000 pairs
drop if merge_shares == 1
* maintain balanced panel
replace aar = `x' if aar == .

save Bartik_data_`x', replace
restore
}

* Next, append all data sets:
use Bartik_data_1999, clear
forvalues x = 2000/2008 {
append using Bartik_data_`x'
}
tostring aar, replace


/*** Load data on WID and transport costs by product-destination-year **/
merge n:1 aar id_proddest using firmsample_prio1000_productdest_importdemand, gen(merge_shocks)

tab aar merge_shocks


rename vareshare_WID share_emp_ind_bhj_
rename importdemand g
gen z_ = share_emp_ind_bhj_ * g
rename g g_

save Bartik_data_prio1000, replace

reshape wide share_emp_ind_bhj_ g z_, i(cvrnr aar) j(id_proddest)

egen z = rowtotal(z_*)

save Bartik_data_wide_prio1000, replace

set more off
use Bartik_data_wide_prio1000, clear

set matsize 3000
* List markets with zero shares
tabstat share_emp_ind* g_*, stats(mean sum)

set more off
cap drop t

rename aar year
destring year, replace
gen counter = 1
local controls sect_* tdummy_*
local weight counter

local y logsales 
local x shock
local z z

local ind_stub share_emp_ind_bhj_
local growth_stub g_

local time_var year
local cluster_var sector027

levelsof `time_var', local(years)

* Drop product-destinations with zero trade shares:
drop g_1 `ind_stub'1 g_19 `ind_stub'19 ///
g_23 `ind_stub'23 g_41 `ind_stub'41 g_42 `ind_stub'42 ///
g_63 `ind_stub'63 ///
g_143 `ind_stub'143 g_191 `ind_stub'191 ///
g_267 `ind_stub'267 g_289 `ind_stub'289 ///
g_352 `ind_stub'352 ///
g_440 `ind_stub'440 g_483 `ind_stub'483 ///
g_485 `ind_stub'485 g_1000 `ind_stub'1000 ///
g_587 `ind_stub'587 g_589 `ind_stub'589 ///
g_831 `ind_stub'831 g_917 `ind_stub'917
 
drop if shock == .

forvalues t = 1999/2008 {
	foreach var of varlist `ind_stub'* {
		gen t`t'_`var' = (year == `t') * `var'
		}
	foreach var of varlist `growth_stub'* {
		gen t`t'_`var'b = `var' if year == `t'
		egen t`t'_`var' = max(t`t'_`var'b), by(cvrnr)
		drop t`t'_`var'b
		}
	}

tab sector009, gen(sect_)
drop sect_1
tab year, gen(tdummy_)
drop tdummy_1

drop if firmcode == .

save Bartik_data_wide_prio1000_temp, replace

set more off
set matsize 11000
use Bartik_data_wide_prio1000_temp, clear
local controls sect_* tdummy_*
local weight counter
local y logsales 
local x shock
local z z
local ind_stub share_emp_ind_bhj_
local growth_stub g_
	
* This loop can be used to extract the separate role of each instrument

areg `y' `x' `controls', absorb(firmcode) cluster(firmcode)

foreach var of varlist `ind_stub'* {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	qui areg `x' `temp' `controls', absorb(firmcode) cluster(firmcode)
	local pi_`ind' = _b[`temp']
	disp `pi_`ind''
	qui test `temp'
	local F_`ind' = r(F)
	disp `F_`ind''	
	qui areg `y' `temp' `controls', absorb(firmcode) cluster(firmcode)
	local gamma_`ind' = _b[`temp']
	drop `temp'
	}

foreach var of varlist `ind_stub'490 `ind_stub'701 `ind_stub'941 `ind_stub'700 `ind_stub'713 `ind_stub'703 `ind_stub'790 {
	if regexm("`var'", "`ind_stub'(.*)") {
		local ind = regexs(1) 
		}
	tempvar temp
	qui gen `temp' = `var' * `growth_stub'`ind'
	ch_weak_bf, p(.1) beta_range(-10(.1)10)   y(`y') x(`x') z(`temp') absorb(firmcode) controls(`controls') cluster(firmcode)
	disp r(beta_min) ,  r(beta_max)
	local ci_min_`ind' =string( r(beta_min), "%9.2f")
	local ci_max_`ind' = string( r(beta_max), "%9.2f")
	disp "`ind', `beta_`ind'', `t_`ind'', [`ci_min_`ind'', `ci_max_`ind'']"
	drop `temp'
	}
	
preserve
keep `ind_stub'* firmcode year `weight'
reshape long `ind_stub', i(firmcode year) j(ind)
gen `ind_stub'pop = `ind_stub'*`weight'
collapse (sd) `ind_stub'sd = `ind_stub' (rawsum) `ind_stub'pop `weight', by(ind year)
tempfile tmp
save tempfile_Bartik_prio1000, replace
restore


local controls sect_* tdummy_*
local weight counter
local y logsales 
local x shock
local z z
local ind_stub share_emp_ind_bhj_
local growth_stub g_

bartik_weight, z(t*_`ind_stub'*)    weightstub(t*_`growth_stub'*) x(`x') y(`y') controls(`controls' ) absorb(firmcode)

mat beta = r(beta)
mat alpha = r(alpha)
mat gamma = r(gam)
mat pi = r(pi)
mat G = r(G)
qui desc t*_`ind_stub'*, varlist
local varlist = r(varlist)

clear
svmat beta
svmat alpha
svmat gamma
svmat pi
svmat G

gen ind = ""
gen year = ""
local t = 1
foreach var in `varlist' {
	if regexm("`var'", "t(.*)_`ind_stub'(.*)") {
		qui replace year = regexs(1) if _n == `t'
		qui replace ind = regexs(2) if _n == `t'
		}
	local t = `t' + 1
	}

destring ind, replace
destring year, replace
merge 1:1 ind year using tempfile_Bartik_prio1000
gen beta2 = alpha1 * beta1
gen indshare2 = alpha1 * (`ind_stub'pop/`weight')
gen indshare_sd2 = alpha1 * `ind_stub'sd
gen G2 = alpha1 * G1
collapse (sum) alpha1 beta2 indshare2 indshare_sd2 G2 (mean) G1 , by(ind)

gen agg_beta = beta2 / alpha1
gen agg_indshare = indshare2 / alpha1
gen agg_indshare_sd = indshare_sd2 / alpha1
gen agg_g = G2 / alpha1

gsort -alpha1
list ind alpha1 if _n<21

cd "${results_dir}"
preserve
count if abs(alpha1)>0.001
tabstat alpha1 agg_beta if abs(alpha1)>0.001, stats(min max sum)
restore
		
capture file close fh
file open fh  using "TableA18.tex", write replace
file write fh "\toprule" _n


/** Panel A: Negative and Positive Weights **/
total alpha1 if alpha1 > 0
mat b = e(b)
local sum_pos_alpha = string(b[1,1], "%9.3f")
total alpha1 if alpha1 < 0
mat b = e(b)
local sum_neg_alpha = string(b[1,1], "%9.3f")

sum alpha1 if alpha1 > 0
local mean_pos_alpha = string(r(mean), "%9.3f")
sum alpha1 if alpha1 < 0
local mean_neg_alpha = string(r(mean), "%9.3f")

local share_pos_alpha = string(abs(`sum_pos_alpha')/(abs(`sum_pos_alpha') + abs(`sum_neg_alpha')), "%9.3f")
local share_neg_alpha = string(abs(`sum_neg_alpha')/(abs(`sum_pos_alpha') + abs(`sum_neg_alpha')), "%9.3f")



/** Panel B: Correlations of Industry Aggregates **/

gen F = .
gen agg_pi = .
gen agg_gamma = .
levelsof ind, local(industries)
foreach ind in `industries' {
	capture replace F = `F_`ind'' if ind == `ind'
	capture replace agg_pi = `pi_`ind'' if ind == `ind'
	capture replace agg_gamma = `gamma_`ind'' if ind == `ind'		
	}

corr alpha1 agg_g agg_beta F agg_indshare_sd
mat corr = r(C)

forvalues i =1/5 {
	forvalues j = `i'/5 {
		local c_`i'_`j' = string(corr[`i',`j'], "%9.3f")
		}
	}


/** Panel C: Top 5 Rotemberg Weight Industries **/
foreach ind in 490 701 941 700 713 {
	qui sum alpha1 if ind == `ind'
   local alpha_`ind' = string(r(mean), "%9.3f")
	qui sum agg_g if ind == `ind'	
	local g_`ind' = string(r(mean), "%9.3f")
	qui sum agg_beta if ind == `ind'	
	local beta_`ind' = string(r(mean), "%9.3f")
	qui sum agg_indshare if ind == `ind'	
	local share_`ind' = string(r(mean)*100, "%9.3f")
	tempvar temp
	qui gen `temp' = ind == `ind'
	gsort -`temp'
	drop `temp'
	}

/** Over ID Figures **/

gen omega = alpha1*agg_beta
total omega
mat b = e(b)
local b = b[1,1]


gen label_var = ind 
gen beta_lab = string(agg_beta, "%9.3f")


gen abs_alpha = abs(alpha1) 
gen positive_weight = alpha1 > 0
gen agg_beta_pos = agg_beta if positive_weight == 1
gen agg_beta_neg = agg_beta if positive_weight == 0

sum agg_beta_pos agg_beta_neg F if F >= 5 & abs(alpha1)>0.00025, det
local b = b[1,1]
disp `b'
twoway (scatter agg_beta_pos agg_beta_neg F if F >= 5 & abs(alpha1)>0.00025 ///
[aweight=abs_alpha ], msymbol(Oh Dh) ), ///
xscale(log) legend(label(1 "Positive Weights") label( 2 "Negative Weights")) ///
yline(`b', lcolor(black) lpattern(dash)) xtitle("First stage F-statistic") ///
graphregion(color(white)) ///
ytitle("{&beta}{subscript:k} estimate") xlabel(10 100 1000 10000 100000 1000000)

graph export "FigureA5.pdf", replace


/** Panel E: Weighted Betas by alpha weights **/
gen agg_beta_weight = agg_beta * alpha1

collapse (sum) agg_beta_weight alpha1 (mean)  agg_beta, by(positive_weight)
egen total_agg_beta = total(agg_beta_weight)
gen share = agg_beta_weight / total_agg_beta
gsort -positive_weight
local agg_beta_pos = string(agg_beta_weight[1], "%9.3f")
local agg_beta_neg = string(agg_beta_weight[2], "%9.3f")
local agg_beta_pos2 = string(agg_beta[1], "%9.3f")
local agg_beta_neg2 = string(agg_beta[2], "%9.3f")
local agg_beta_pos_share = string(share[1], "%9.3f")
local agg_beta_neg_share = string(share[2], "%9.3f")

/*** Write final table **/
/** Panel A **/
file write fh "\multicolumn{3}{l}{\textbf{Panel A: Negative and positive weights}}\\" _n
file write fh  " & Sum & Mean & Share \\  \cmidrule(lr){2-4}" _n
file write fh  "Negative & `sum_neg_alpha' & `mean_neg_alpha' & `share_neg_alpha' \\" _n
file write fh  "Positive & `sum_pos_alpha' & `mean_pos_alpha' & `share_pos_alpha' \\" _n

/** Panel B **/
file write fh "\multicolumn{5}{l}{\textbf{Panel B: Correlations of Industry Aggregates} }\\" _n
file write fh  " &$\alpha_k$ & \$g_{k}$ & $\beta_k$ & \$F_{k}$ & Var(\$z_k$) \\" _n
file write fh  "\cmidrule(lr){2-6} " _n
file write fh " & \\" _n
file write fh " $\alpha_k$             & 1\\" _n
file write fh " \$g_{k}$                &   `c_1_2'  & 1\\" _n
file write fh " $\beta_{k}$             &   `c_1_3'  & `c_2_3'    &1\\" _n
file write fh " \$F_{k}$                &   `c_1_4'  & `c_2_4'    &  `c_3_4'  & 1\\" _n
file write fh " Var(\$z_{k}$)           &   `c_1_5'  & `c_2_5'    &  `c_3_5'  &  `c_4_5'   &1\\" _n

/** Panel C **/
file write fh "\multicolumn{5}{l}{\textbf{Panel D: Top 5 Rotemberg weight industries} }\\" _n
file write fh  " & $\hat{\alpha}_{k}$ & \$g_{k}$ & $\hat{\beta}_{k}$ & 95 \% CI & Ind Share \\ \cmidrule(lr){2-6}" _n
foreach ind in 490 701 941 700 713 {
	if `ci_min_`ind'' != -10 & `ci_max_`ind'' != 10 {
		file write fh  "`ind' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & (`ci_min_`ind'',`ci_max_`ind'')  & `share_`ind'' \\ " _n
		}
	else  {
		file write fh  "`ind' & `alpha_`ind'' & `g_`ind'' & `beta_`ind'' & \multicolumn{1}{c}{N/A}  & `share_`ind'' \\ " _n
		}
	}

/** Panel D **/
file write fh "\multicolumn{5}{l}{\textbf{Panel E: Estimates of $\beta_{k}$ for positive and negative weights} }\\" _n
file write fh  " & $\alpha$-weighted Sum & Share of overall $\beta$ & Mean  \\ \cmidrule(lr){2-3}" _n
file write fh  " Negative & `agg_beta_neg' & `agg_beta_neg_share' &`agg_beta_neg2' \\" _n
file write fh  " Positive & `agg_beta_pos' & `agg_beta_pos_share' & `agg_beta_pos2' \\" _n
file write fh  "\bottomrule" _n
file close fh


	
	
	
	

